package com.my.algorithmlearning.leetcode;

import com.my.algorithmlearning.entity.TreeNode;
import org.springframework.util.Assert;

/**
 * @ClassName BinaryTreeIsSymmetricV1_Bad
 * @Description BinaryTreeIsSymmetricV1_Bad
 * @Author wanghaiwei
 * @Date 2022/11/24 13:49
 */
public class BinaryTreeIsSymmetricV2_NormalRecursive {
    public boolean isSymmetric(TreeNode root) {
        return this.check(root,root);
    }

    private boolean check(TreeNode p, TreeNode q) {
        if(p == null && q == null) {
            return true;
        }
        if (p == null || q == null) {
            return false;
        }
        return p.val == q.val && check(p.left, q.right) && check(p.right, q.left);
    }


    public static void main(String[] args) {
        BinaryTreeIsSymmetricV2_NormalRecursive solution = new BinaryTreeIsSymmetricV2_NormalRecursive();
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(2);
        root.left.left = new TreeNode(3);
        root.left.right = new TreeNode(4);
        root.right.left = new TreeNode(4);
        root.right.right = new TreeNode(3);
        Assert.isTrue(solution.isSymmetric(root), "[1,2,2,3,4,4,3]结果计算错误");

        TreeNode root2 = new TreeNode(1);
        root2.left = new TreeNode(2);
        root2.left.right = new TreeNode(3);
        root2.right = new TreeNode(2);
        root2.right.right = new TreeNode(3);
        Assert.isTrue(!solution.isSymmetric(root2), "[1,2,2,null,3,null,3]结果计算错误");

        TreeNode root3 = new TreeNode(1);
        root3.left = new TreeNode(2);
        root3.left.left = new TreeNode(2);
        root3.right = new TreeNode(2);
        root3.right.left = new TreeNode(2);
        Assert.isTrue(!solution.isSymmetric(root3), "[1,2,2,2,null,2]结果计算错误");
    }
}
